package example

import (
	"context"
	"fmt"
	"github.com/beherochuling/beesoft_api/app/helper/util"
	"google.golang.org/grpc/metadata"
	"io"
	"log"
	"time"
)

type User struct{}

// type ServerSide struct{}
// type ClientSide struct{}
// type Bidirectional struct{}

func (r *User) GetUser1(ctx context.Context, req *pb.UserRequest) (resp *pb.UserResponse, err error) {
	err = util.CheckToken(ctx)
	if err != nil {
		return nil, err
	}

	// return nil,status.Errorf(codes.Unavailable, "failed please try again")

	if req.Name == "test" {
		resp = &pb.UserResponse{
			Id:    req.Id,
			Name:  req.Name,
			Age:   19,
			Hobby: []string{"FanOne", "FanOneTwo"},
		}
	}
	err = nil
	return
}
func (r *User) GetUser2(req *pb.GetUserRequest, stream pb.User_GetUser2Server) error {
	for i := 0; i < 10; i++ {
		time.Sleep(time.Second)

		err := stream.Send(&pb.GetUserResponse{
			UserId: fmt.Sprintf("%d", i),
			Name:   "name" + fmt.Sprintf("%d", i),
			Email:  fmt.Sprintf("test@163.com"),
		})
		if err != nil {
			log.Fatalf("%v.Send(%v) = %v", stream, req, err)
		}
	}

	return nil
}
func (r *User) GetUser3(stream pb.User_GetUser3Server) error {
	for {
		rev, err := stream.Recv()
		fmt.Printf("recv client data:%s\n", rev.GetUserId())

		if err == io.EOF {
			time.Sleep(time.Second)
			var res pb.GetUserResponse
			res.UserId = "123"
			res.Name = "test"
			res.Email = "test@163.com"
			return stream.SendAndClose(&res)
		}
		if err != nil {
			log.Fatalf("%v.GetUser(_) = _, %v", r, err)
		}
	}
}
func (r *User) GetUser4(stream pb.User_GetUser4Server) (err error) {
	for {
		req, err := stream.Recv()
		if err != nil {
			log.Printf("stream.Recv error: %v", err)
			return err
		}
		log.Printf("backend receive data: %v", req)
		err = stream.Send(&pb.GetUserResponse{
			UserId: req.UserId,
			Name:   "test",
			Email:  "test@163.com",
		})
		if err != nil {
			log.Printf("stream.Send error: %v", err)
			return err
		}
	}

	// wg := sync.WaitGroup{}
	// defer wg.Wait()
	// wg.Add(2)
	//
	// go func() {
	// 	defer  wg.Done()
	// 	for i:=0;i<10;i++ {
	// 		data,_ := alldata.Recv()
	// 		fmt.Println("AllStream收到客户端消息",data)
	// 	}
	// }()
	//
	// go func() {
	// 	defer  wg.Done()
	// 	for i:=0;i<10;i++ {
	// 		_ = alldata.Send(&streamprotodemo.StreamResData{
	// 			Data: "我是服务器",
	// 		})
	// 		time.Sleep(time.Second)
	// 	}
	// }()
	//
	// return nil
}

func (r *User) GetUser5() {
	// req := &zaproto.SearchRequestParam{
	// 	QueryText: "xxxxxx",
	// 	Limit:     10,
	// 	Type:      zaproto.SearchRequestParam_PC,
	// }
	//
	// data, err := proto.Marshal(req)
	// if err != nil {
	// 	panic(err)
	// }
	//
	// fmt.Println(string(data))
	//
	// var respData []byte
	// var result = zaproto.SearchResultPage{}
	// err = proto.Unmarshal(respData, &result)
	// if err == nil {
	// 	panic(err)
	// }
	//
	// fmt.Println(result)
}

func (r *User) GetUser6(ctx context.Context) {
	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		panic("获取md失败")
	}

	if name, ok := md["name"]; ok {
		fmt.Println(name[0])
	}

	// if statusinfo, ok := status.FromError(err); ok {
	// 	fmt.Println(statusinfo.Code())
	// 	fmt.Println(statusinfo.Message())
	// }

	// status.Error(codes.Internal, "内部错误")
}
